#pragma once

#include <iostream>
#include"TcpServer.hpp"
#include"Protocol.hpp"
#include"Log.hpp"
#include"Task.hpp"
#include"ThreadPool.hpp"
#define PORT 8081


class HttpServer
{
private:
    int port_;
    bool stop;
public:
    HttpServer(int port)
    :port_(port),stop(false)
    {}
    void InitServer()
    {
        signal(SIGPIPE,SIG_IGN);
    }
    void loop()
    {
        TcpServer *tsvr = TcpServer::getinstance(port_);
        LOG(INFO,"loop begin");
        while(!stop)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(tsvr->Sock(),(struct sockaddr*)&peer,&len);
            if(sock<0)
            {
                continue;
            }
            LOG(INFO,"get a new link ...");
            Task task(sock);
            ThreadPool::getinstance()->PushTask(task);
        }
    }
    
    ~HttpServer()
    {}
};